package com.woniuxy.services.impl;

import com.github.pagehelper.PageHelper;
import com.woniuxy.daos.TbBedDao;
import com.woniuxy.daos.TbMalfunctionDao;
import com.woniuxy.daos.TbSectionDao;
import com.woniuxy.daos.TbUserRoleDao;
import com.woniuxy.entitys.*;
import com.woniuxy.services.FaultService;
import com.woniuxy.utils.LoginUser;
import com.woniuxy.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 故障列表
 */
@Service
public class FaultServiceImpl implements FaultService {

    @Autowired
    private TbMalfunctionDao tbMalfunctionDao;
    @Autowired
    private TbUserRoleDao tbUserRoleDao;
    @Autowired
    private UserUtils userUtils;
    @Autowired
    private TbSectionDao tbSectionDao;
    @Autowired
    private TbBedDao tbBedDao;

    @Override
    public List<TbMalfunction> getAllFault(Integer pageNum, Integer pageSize, Integer faultStatus) {
        //得到该用户的LoginUser对象
        LoginUser loginUser = userUtils.getLoginUser();
        Integer uid = loginUser.getUser().getUid();
        TbUserRoleExample tbUserRoleExample = new TbUserRoleExample();
        TbUserRoleExample.Criteria criteria = tbUserRoleExample.createCriteria();
        criteria.andUidEqualTo(uid);
        //查询用户角色
        TbUserRole tbUserRole = tbUserRoleDao.selectByExample(tbUserRoleExample).get(0);
        PageHelper.startPage(pageNum, pageSize);
        if(tbUserRole.getRid() == 8) {//科室负责人
            //根据科室负责人的uid查询自己科室下的所有故障记录
            return tbMalfunctionDao.getAllFaultByHid(uid, faultStatus);
        } else {//管理员等
            return tbMalfunctionDao.getAllFault(faultStatus);
        }
    }

    @Override
    public TbMalfunction getAllFaultById(Integer maid) {
        return tbMalfunctionDao.getAllFaultById(maid);
    }

    @Override
    @Transactional
    public boolean addFault(TbMalfunction tbMalfunction) {
        tbMalfunction.setDeclarantStatus("0");
        tbMalfunction.setDeclarantTime(new Date());
        if(tbMalfunctionDao.insert(tbMalfunction) > 0) {
            TbBed tbBed = new TbBed();
            tbBed.setBid(tbMalfunction.getBid());
            tbBed.setStatus(2);
            tbBedDao.updateByPrimaryKeySelective(tbBed);
        }
        return true;
    }

    @Override
    public boolean updateFault(TbMalfunction tbMalfunction) {
        return tbMalfunctionDao.updateByPrimaryKeySelective(tbMalfunction) > 0;
    }

    @Override
    public TbSection getSectionInfo(Integer uid) {
        return tbSectionDao.getSectionInfoByUid(uid);
    }

    @Override
    public List<TbBed> getNoFaultBed(Integer hid) {
        TbBedExample tbBedExample = new TbBedExample();
        TbBedExample.Criteria criteria = tbBedExample.createCriteria();
        criteria.andStatusNotEqualTo(2).andDidEqualTo(hid);
        return tbBedDao.selectByExample(tbBedExample);
    }

    @Override
    public List<TbMalfunction> getAllFaultByFuzz(Integer pageNum, Integer pageSize, String searchValue) {
        //得到该用户的LoginUser对象
        LoginUser loginUser = userUtils.getLoginUser();
        Integer uid = loginUser.getUser().getUid();
        TbUserRoleExample tbUserRoleExample = new TbUserRoleExample();
        TbUserRoleExample.Criteria criteria = tbUserRoleExample.createCriteria();
        criteria.andUidEqualTo(uid);
        //查询用户角色
        TbUserRole tbUserRole = tbUserRoleDao.selectByExample(tbUserRoleExample).get(0);
        PageHelper.startPage(pageNum, pageSize);
        if(tbUserRole.getRid() == 8) {//科室负责人
            //根据科室负责人的uid查询自己科室下的所有故障记录
            return tbMalfunctionDao.getAllFaultByFuzzAndHid(uid, searchValue);
        } else {//管理员等
            return tbMalfunctionDao.getAllFaultByFuzz(searchValue);
        }
    }

    @Override
    public List<TbMalfunction> getFaultList() {
        return tbMalfunctionDao.selFaultList();
    }
    public List<TbMalfunction> getAllFaultByCascade(Integer pageNum, Integer pageSize, List<Integer> cascadeValue) {
        PageHelper.startPage(pageNum, pageSize);
        return tbMalfunctionDao.getAllFaultByCascade(cascadeValue.get(0), cascadeValue.get(1));
    }

    @Override
    public boolean acceptFailure(List<Integer> list) {
        TbMalfunctionExample tbMalfunctionExample = new TbMalfunctionExample();
        TbMalfunctionExample.Criteria criteria = tbMalfunctionExample.createCriteria();
        criteria.andMaidIn(list);
        TbMalfunction tbMalfunction = new TbMalfunction();
        tbMalfunction.setDeclarantStatus("1");
        return tbMalfunctionDao.updateByExampleSelective(tbMalfunction, tbMalfunctionExample) > 0;
    }

    @Override
    public boolean cancelFailure(List<Integer> list, List<Integer> bids) {
        TbMalfunctionExample tbMalfunctionExample = new TbMalfunctionExample();
        TbMalfunctionExample.Criteria criteria = tbMalfunctionExample.createCriteria();
        criteria.andMaidIn(list);
        tbMalfunctionDao.deleteByExample(tbMalfunctionExample);
        TbBedExample tbBedExample = new TbBedExample();
        TbBedExample.Criteria criteria2 = tbBedExample.createCriteria();
        criteria2.andBidIn(bids);
        TbBed tbBed = new TbBed();
        tbBed.setStatus(0);
        tbBedDao.updateByExampleSelective(tbBed, tbBedExample);
        return true;
    }
}
